//
//  FinishPersonalView.m
//  Findif
//
//  Created by Mac on 2019/4/24.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "FinishPersonalView.h"

@interface FinishPersonalView ()<UITextFieldDelegate>

//头部
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, strong)UIImageView * nameImageView;
@property (nonatomic, strong)UIImageView * IDCardImageView;
@property (nonatomic, strong)UITextField * nameTextField;
@property (nonatomic, strong)UITextField * IDCardTextField;
@property (nonatomic, weak)UIButton * cancelButton;
@property (nonatomic, weak)UIButton * sureButton;

@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;

@property (nonatomic, copy)NSString * id_card;
@property (nonatomic, copy)NSString * id_card_complete;
@property (nonatomic, copy)NSString * name;

@end

@implementation FinishPersonalView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
        [self loadData];
    }
    
    return self;
}


- (void)loadData{
    
    NSString *ID = DEF_GET_OBJECT(@"id");
    
    [[ZRNetworkTool sharedTool] getUserInfoResultWithUser_id:ID WithExamine_user_id:@"" WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            
            if (dict[@"real_name"]) {
                
                self.nameTextField.text = dict[@"real_name"];
                self.name = dict[@"real_name"];
            }
            
            if (dict[@"id_card"]) {
                
                self.ID = @"yes";
                self.IDCardTextField.text = dict[@"id_card"];
                self.id_card = dict[@"id_card"];
                self.id_card_complete = dict[@"id_card_complete"];
            }
        }
    }];
}


- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"完善个人信息";
    titleLabel.textColor = ZRBlackColor;
    titleLabel.font = [UIFont systemFontOfSize:18 * SCREENH6];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel = titleLabel;
    
    [self addSubview:self.titleLabel];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.textColor = [UIColor lightGrayColor];
    exploreLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    exploreLabel.text = @"请您完善个人信息便于我们为您购买个人出行保险";
    exploreLabel.textAlignment = NSTextAlignmentCenter;
    
    self.exploreLabel = exploreLabel;
    
    [self addSubview:self.exploreLabel];
    
    self.nameImageView = [self setupImageViewWithImg:@"姓名"];
    [self addSubview:self.nameImageView];
    self.IDCardImageView = [self setupImageViewWithImg:@"身份证"];
    [self addSubview:self.IDCardImageView];
    
    self.nameTextField = [self setupTextFiledWith:@"填写真实姓名"];
    [self addSubview:self.nameTextField];
    self.IDCardTextField = [self setupTextFiledWith:@"填写身份证号"];
    [self addSubview:self.IDCardTextField];
    
    self.oneLineView = [self setupLineView];
    [self addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];

    UIButton * cancelButton = [UIButton new];
    
    [cancelButton setTitle:@"修改信息" forState:UIControlStateNormal];
    [cancelButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.backgroundColor = ColorWithRGB(0xD5D5D5);
    cancelButton.layer.cornerRadius = 5 * SCREENH6;
    cancelButton.layer.masksToBounds = YES;
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.cancelButton = cancelButton;
    
    [self addSubview:self.cancelButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"确认提交" forState:UIControlStateNormal];
    [sureButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    sureButton.backgroundColor = ColorWithRGB(0xFFE135);
    sureButton.layer.cornerRadius = 5 * SCREENH6;
    sureButton.layer.masksToBounds = YES;
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self addSubview:self.sureButton];
}


- (void)layoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(20 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.nameImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(20 * SCREENH6);
        make.top.mas_equalTo(self.exploreLabel.mas_bottom).offset(30 * SCREENH6);
        make.width.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameImageView.mas_trailing).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-20 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.centerY.mas_equalTo(self.nameImageView);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.nameTextField);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.IDCardImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.nameImageView);
        make.top.mas_equalTo(self.nameImageView.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.IDCardTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameTextField);
        make.centerY.mas_equalTo(self.IDCardImageView);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.IDCardTextField);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(60 * SCREENH6);
        make.top.mas_equalTo(self.IDCardImageView.mas_bottom).offset(30 * SCREENH6);
        make.width.mas_equalTo(90 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.cancelButton);
        make.trailing.mas_equalTo(self).offset(-60 * SCREENH6);
    }];
}

- (UIImageView *)setupImageViewWithImg:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    
    return imageView;
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xABABAB);
    
    return lineView;
}

- (UITextField *)setupTextFiledWith:(NSString *)holder{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = ZRWhiteColor;
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xABABAB);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:holder attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.textAlignment = NSTextAlignmentCenter;
    textFiled.delegate = self;
    
    return textFiled;
}


- (void)clickButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.cancelButton) {
        
//        type = @"取消";
//        NSDictionary * dict;
//        if (self.delegate && [self.delegate respondsToSelector:@selector(clickButtonWithType: WithDict:)])
//        {
//            [self.delegate clickButtonWithType:type WithDict:dict];
//        }
        
        self.IDCardTextField.text = self.id_card_complete;
        [self.IDCardTextField becomeFirstResponder];
    }else{
        
        if (self.nameTextField.text.length > 0 && self.IDCardTextField.text.length > 0) {
            
            type = @"sure";
            NSDictionary * dict = @{@"id_card":self.IDCardTextField.text,@"real_name":self.nameTextField.text};
            if (self.delegate && [self.delegate respondsToSelector:@selector(clickButtonWithType: WithDict:)])
            {
                [self.delegate clickButtonWithType:type WithDict:dict];
            }
        }else{
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(firstFinishPersonalInfo)])
            {
                [self.delegate firstFinishPersonalInfo];
            }
        }
        
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (self.ID) {
        
        if ([self.IDCardTextField.text isEqualToString:self.id_card_complete] && [self.nameTextField.text isEqualToString:self.name]) {
            
            self.ID = @"yes";
        }else{
            
            self.ID = @"no";
        }
    }
}



@end
